package com.zq.solution.string;

/**
 * @ClassName CommonPrefix
 * @Description 求字符串数组中的公共前缀
 * @Author ZQ
 * @Date 2020/10/12 20:03
 * @Version 1.0
 */
public class CommonPrefix {
    static String commonPrefix(String[] strs){
        if (strs == null || strs.length == 0)   return "";
        String prefix = strs[0];
        for (int i = 1; i < strs.length; i++){
            prefix = commonPrefix(prefix,strs[i]);
            if (prefix.length() == 0){
                break;
            }
        }
        return prefix;
    }

    static String commonPrefix(String s1, String s2){
        int length = Math.min(s1.length(),s2.length());
        int i = 0;
        while (i < length && s1.charAt(i) == s2.charAt(i)){
            i++;
        }
        return s1.substring(0,i);
    }

    public static void main(String[] args) {
        String[] strs = new String[]{"flower","flow","flight"};
        System.out.println(commonPrefix(strs));
    }
}
